<?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Support\Facades\Auth;

    class AdminUsersRelatedModel extends Model
    {
        protected $table = 'admin_users_related';

        protected $fillable
            = [
                'id',
                'user_id',
                'audit_level',
                'qq',
                'mobile',
                'id_number',
                'service_date',
                'home_address',
                'living_address',
                'remark',
                'created_at',
                'updated_at',
            ];

        public function stuff(): array
        {
            $stuff = self::query()->leftJoin('admin_users', 'admin_users_related.user_id', '=', 'admin_users.id');
            if (ISADMIN) $stuff->where('admin_users_related.main_id', '>', 0);
            else {
                if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) {
                    $stuff->orWhere('admin_users_related.main_id', MAINID)
                        ->orWhere('user_id', MAINID);
                }
                else $stuff->where('admin_users_related.user_id', USERID);
            }
            $stuff = $stuff->select(
                'user_id',
                'admin_users.name'
            )->get()->toArray();
            $arr = [];
            foreach ($stuff as $value) {
                $arr[$value['user_id']] = $value['name'];
            }

            return $arr;
        }
    }
